package cn.openbiotoken.impl;

import cn.openbiotoken.Biometrics;
import cn.openbiotoken.Gender;
import cn.openbiotoken.GenderClassification;

import static cn.openbiotoken.Biometrics.DEFAULT_CONFIDENCE_PERCENTAGE;
import static cn.openbiotoken.Biometrics.MAX_CONFIDENCE_PERCENTAGE;
import static cn.openbiotoken.Biometrics.MIN_CONFIDENCE_PERCENTAGE;
import static java.lang.String.format;

public class GenderClassificationImpl implements GenderClassification {

    public GenderClassificationImpl(final Gender gender, final byte confidencePercentage) {
        super();

        if (null==gender) {
            throw new IllegalArgumentException("The gender should be present.");
        }

        if (confidencePercentage>MAX_CONFIDENCE_PERCENTAGE) {
            throw new IllegalArgumentException(format("The confidence percentage should be less than or equal to %d. Actual confidence percentage is %d.", MAX_CONFIDENCE_PERCENTAGE, confidencePercentage));
        }
        if (confidencePercentage<MIN_CONFIDENCE_PERCENTAGE) {
            throw new IllegalArgumentException(format("The confidence percentage should be greater than or equal to %d. Actual confidence percentage is %d.", MIN_CONFIDENCE_PERCENTAGE, confidencePercentage));
        }

        this.gender = gender;
        this.confidencePercentage = confidencePercentage;
    }

    public GenderClassificationImpl(final int gender, final int confidencePercentage) {
        this(GenderClassification.legalizeGender(gender), Biometrics.legalizeConfidencePercentage(confidencePercentage));
    }

    public GenderClassificationImpl(final int gender) {
        this(gender, DEFAULT_CONFIDENCE_PERCENTAGE);
    }

    private final Gender gender;

    private final byte confidencePercentage;

    @Override
    public Gender getGender() {
        return gender;
    }

    @Override
    public byte getConfidencePercentage() {
        return confidencePercentage;
    }
}
